package edu.ycxy.poetry.controller;


import com.github.pagehelper.PageInfo;
import edu.ycxy.poetry.entity.ZhiShi;
import edu.ycxy.poetry.service.IZhiShiService;
import edu.ycxy.poetry.vo.AddKnowVo;
import edu.ycxy.poetry.vo.UpdateKnowVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author @xin
 * @createDate 2022/3/8
 * @Description
 */
@RestController
@RequestMapping("/know")
@Slf4j
public class ZhiShiController {

    @Autowired
    private IZhiShiService zhiShiService;

    //根据id倒序查询知识表中的所有内容 , 并进行分页展示
    @RequestMapping("/selectList")
    public PageInfo<ZhiShi> SelectList(Integer pageNum){
        if (pageNum == null){
            pageNum = 1;
        }
        //对查询出的结果进行分页展示,每页的数据为 10 条
        PageInfo<ZhiShi> zhiShiPageInfo = zhiShiService.selectList(pageNum, 10);
        log.debug("查询出知识的结果集为:"+zhiShiPageInfo);
        return zhiShiPageInfo;
    }

    //根据知识的标题查询知识的详细信息
    @RequestMapping("/selectOne/{title}")
    public ZhiShi SelectOneByTitle(@PathVariable("title") String title){
        log.debug("查询出的具体知识内容:"+zhiShiService.selectByTitle(title));
        return zhiShiService.selectByTitle(title);
    }

    // 添加知识
    @RequestMapping("/addKnow")
    public String addKnow(@RequestBody AddKnowVo addKnowVo){
        String s = zhiShiService.addKnow(addKnowVo);
        if (s.equals("插入成功")){
            return "ok";
        }else {
            return "fail";
        }
    }

    //对标题进行模糊查询
    @RequestMapping("/fuzzy/search/{title}")
    public PageInfo<ZhiShi> findListByTitle(@PathVariable("title") String title,Integer pageNum){
        log.debug("获取的搜索内容： " + title);
        if (pageNum == null){
            pageNum = 1;
        }
        return zhiShiService.findListByTitle(title,pageNum,10);
    }

    @RequestMapping("/delete/{id}")
    public void DeleteById(@PathVariable("id") Integer id){
        zhiShiService.DeleteById(id);
    }

    @RequestMapping("/findOne/{id}")
    public UpdateKnowVo findOneById(@PathVariable Integer id){
        return zhiShiService.findOneById(id);
    }

    @RequestMapping("/update")
    public Integer updateKnowById(@RequestBody UpdateKnowVo updateKnowVo){
        Integer integer = zhiShiService.UpdateKnowById(updateKnowVo);
        if (integer == 1){
            return 1;
        }else {
            return 2;
        }
    }
}
